package com.sg.hms.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sg.hms.Model.Employee;
import com.sg.hms.Model.Role;
import com.sg.hms.Model.User;
import com.sg.hms.Service.DepartmentService;
import com.sg.hms.Service.EmployeeService;
import com.sg.hms.Service.UserService;
import com.sg.hms.ServiceImpl.DepartmentServiceImpl;
import com.sg.hms.ServiceImpl.EmployeeServiceImpl;
import com.sg.hms.ServiceImpl.UserServiceImpl;
/**
 * 
 * @author 王奇霞
 *
 */
public class LoginController extends HttpServlet{
     private UserService uservice=new UserServiceImpl();
     private EmployeeService emservice=new EmployeeServiceImpl();
     private DepartmentService dService=new DepartmentServiceImpl();
	@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	doPost(req, resp);
    }
     @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	req.setCharacterEncoding("utf-8");
    	PrintWriter pw=resp.getWriter();
    	HttpSession session=req.getSession();
    	String uId=req.getParameter("uId").trim();
    	String upsw=req.getParameter("upsw");
    	User user=uservice.selectUserByUnameUpsw(uId, upsw);//获得当前用户
    	Role role=uservice.selectRoleByroleId(user.getRoleId());//获得当前用户的角色信息
    	Employee em=emservice.selectEmployeeByElId(user.getuId());//获得当前用户对象的员工信息
    	if(null!=user){
    		String departmentName=dService.selectDeNameBydId(em.getdId());
    		session.setAttribute("currentuser", user);
    		session.setAttribute("currentrole", role);
    		session.setAttribute("currentemployee", em);
    		session.setAttribute("departmentName", departmentName);//当前用户的部门名
    	   pw.write("1");
    	}else{
    		pw.write("0");
    	}

    }
}
